package com.mingqijia.gassafety.job.service.cmp;

import com.alibaba.fastjson.JSONObject;
import com.mingqijia.gassafety.job.response.ConsoleSkSupplierListResponse;
import com.mingqijia.gassafety.shared.constant.Constants;
import com.mingqijia.gassafety.shared.constant.UrlConst;
import com.mingqijia.gassafety.shared.utils.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author zhangfei
 * @version 1.0.0
 * @ClassName EquipmentCmpService.java
 * @createTime 2022年08月08日
 */
@Component
@Slf4j
public class EquipmentCmpService {

    @Value(value = "${gassafety.cmp.url}")
    public String targetUrl;

    @Value(value = "${gassafety.cmp.appId}")
    public String appId;

    @Autowired
    HttpUtil httpUtil;

    @Autowired
    RedisTemplate redisTemplate;



    /**
     * cmp请求
     * @return
     */
    public ConsoleSkSupplierListResponse supplierListRequest() {
        ConsoleSkSupplierListResponse listResponse = new ConsoleSkSupplierListResponse();
        try {
            String rKey = Constants.EQUIPMENT_MANUFACTURER_REDIS_KEY;
            Object obj = redisTemplate.opsForValue().get(rKey);
            String body = obj==null?"":obj.toString();
            if (StringUtils.isEmpty(body)) {
                Map<String, Object> jsonData = new HashMap<>();
                jsonData.put("supplierType", "4");
                log.info("supplierListRequest——cmpRequest,主要发送数据：{},type：{}",
                        JSONObject.toJSONString(jsonData));
                String url = targetUrl + UrlConst.QUERY_SUPPLIER_LIST.getUrl();
                log.info("supplierListRequest——url：{}", url);
                String jsonStr = JSONObject.toJSONString(jsonData);
                body = HttpUtil.getSupplierBody(jsonStr, url, "", getSupplierHeaders());
                log.info("supplierListRequest——响应结果body：{}",
                        JSONObject.toJSONString(body));
                listResponse = JSONObject.parseObject(body, ConsoleSkSupplierListResponse.class);
                if (listResponse != null && listResponse.getCode() == 0 &&
                        listResponse.getData() != null && !listResponse.getData().isEmpty() ) {
                    redisTemplate.opsForValue().set(rKey,body,20*60, TimeUnit.SECONDS);
                }
            }else{
                log.info("supplierListRequest——redis缓存中的响应结果body：{}",
                        JSONObject.toJSONString(body));
                listResponse = JSONObject.parseObject(body, ConsoleSkSupplierListResponse.class);
            }
        } catch (Exception e) {
            log.error("supplierListRequest-error:{}", e);
            throw e;
        }
        return listResponse;
    }

    public Map<String, String> getSupplierHeaders() {
        Map<String, String> headers = new HashMap<>();
        headers.put("APP-Id", appId);
        headers.put("Request-Id", UUID.randomUUID().toString().replaceAll("-", ""));
        log.info("getHeaders-headers:{}",JSONObject.toJSONString(headers));
        return headers;
    }
}